/**
 * @author:albert
 */

package com.bkbw.live.battle.business.ai;

import java.util.Iterator;
import java.util.LinkedList;

import com.bkbw.live.battle.business.UnitObject;

public class GoalThink<EntityType extends UnitObject> extends GoalComposite<EntityType>{
	protected LinkedList<GoalEvaluator<EntityType>> evaluators;
	public GoalThink(EntityType entity) {
		super(entity);
	}
	
	public void arbitrate() {
		float best = 0;
		GoalEvaluator<EntityType> mostDesirable = null;
		Iterator<GoalEvaluator<EntityType>> iterator = evaluators.iterator();
		while (iterator.hasNext()) {
			GoalEvaluator<EntityType> evaluator = iterator.next();
			float score = evaluator.calculateScore(owner);
			if (score >= best){
				best = score;
				mostDesirable = evaluator;
			}
		}
		mostDesirable.setGoal(owner);
	}
	
	
	// ------------ Override from Goal -------------
	@Override
	public void activate() {
		if (owner.isMonster()) {
			arbitrate();
		}
		
		status = Status.Active;
	}
	
	@Override
	public Status process() {
		activateIfInactive();
		Status subgoalStatus = processSubgoals();
		if (subgoalStatus == Status.Completed || subgoalStatus == Status.Failed) {
			if (owner.isMonster()) {
				status = Status.Inactive;
			}
		}
		return status;
	}
	
	@Override
	public void terminate() {
		
	}
	
	// ------------
}
